package com.jike.mahjong.service.Imp;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jike.mahjong.common.Constants;
import com.jike.mahjong.common.Page;
import com.jike.mahjong.common.util.HttpUtil;
import com.jike.mahjong.common.util.TransSerialNumTool;
import com.jike.mahjong.dao.PlayerDao;
import com.jike.mahjong.model.CoinRecordModel;
import com.jike.mahjong.model.Player;
import com.jike.mahjong.model.RequestMethod;
import com.jike.mahjong.model.RequestModel;
import com.jike.mahjong.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * player  service
 * Created by star on 2016/10/11.
 */
@Service("playerService")
public class PlayerServiceImp implements PlayerService {


    @Autowired
    private PlayerDao playerDao;

    /**
     * 查询player
     *
     * @param para
     * @return
     */
    public Page queryPlayerInfo(Map<String, Object> para,Page page) {
        int offset=(page.getCurrentPage()-1)*page.getPageSize();
        para.put("offset",offset);
        para.put("pageSize",page.getPageSize());
        List<Player>  pageList=playerDao.queryPlayerInfo(para);
        int playerCount=playerDao.countQueryPlayerInfo(para);
        page.setTotalCount(playerCount);
        page.setData(pageList);
        return page;
    }

    /**
     * 充值
     *
     * @return
     */
    @Override
    public Integer addCoin(Map<String,Object> para,String id,String addCoin) throws Exception {

        CoinRecordModel coin=new CoinRecordModel();
        List<String> args=new ArrayList<String>();
        RequestModel requestModel=new RequestModel();
        requestModel.setCmd(RequestMethod.addDiamond);
        args.add(id);
        args.add(addCoin.trim());
        requestModel.setArgs(args);

        int resultUpdate = 0;
        if(Constants.envFlag){
        	Gson gson=new Gson();
        	String paraStr= gson.toJson(requestModel);
        	String result= HttpUtil.PostWithJson(Constants.BACK_URL,paraStr);
        	JsonParser jsonParser=new JsonParser();
        	JsonObject jsonObject=jsonParser.parse(result).getAsJsonObject();
        	resultUpdate=Integer.parseInt(String.valueOf(jsonObject.get("res")));
        }else{
        	resultUpdate=1;
        	System.err.println("测试环境,未走接口");
        }
        if(resultUpdate>0){
        	coin.setRecordId(TransSerialNumTool.createSerialNum());
            coin.setMahjongCoin(Integer.valueOf(addCoin));
            coin.setPlayerId(id);
            coin.setManagerId(para.get("managerId")+"");
            coin.setAmount(Integer.parseInt(para.get("amount")+""));
            coin.setPlayerRole(Constants.PLAYER_ROLE);
            playerDao.insertRechargeRecord(coin);
        }

        return resultUpdate;
    }


    @Override
    public Player queryPlayInfoById(Map<String, Object> map) {
        return playerDao.queryPlayerOtherById(map);
    }

    @Override
    public Integer setManagerRole(Map<String, Object> para) {
        return playerDao.setManagerRole(para);
    }

    @Override
    public Integer updatePlayerState(String playerId,String  sign) {
        Gson gson=new Gson();
        Map<String, Object> paras=new HashMap<String, Object>();
        if(sign.equals("1")){
            paras.put("cmd", RequestMethod.lockPlayer);
        }
        if(sign.equals("0")){
            paras.put("cmd", RequestMethod.unLockPlayer);
        }
        String[] args=playerId.split(",");
        paras.put("args",args);

       String paraStr= gson.toJson(paras);
       if(Constants.envFlag){
			String result= HttpUtil.PostWithJson(Constants.BACK_URL,paraStr);
			JsonParser jsonParser=new JsonParser();
			JsonObject jsonObject=jsonParser.parse(result).getAsJsonObject();
			return Integer.parseInt(String.valueOf(jsonObject.get("res")));
       }
       return 1;
    }


    /**
     * 查询充值记录
     *
     * @param para
     * @return
     */
    public Page queryPlayerRechargeRecord(Map<String, Object> para,Page page) {

        int offset=(page.getCurrentPage()-1)*page.getPageSize();
        para.put("offset",offset);
        para.put("pageSize",page.getPageSize());
        List<CoinRecordModel> recordModelList=playerDao.queryRechargeRecord(para);
        int recordCount=playerDao.countRechargeRecord(para);
        page.setTotalCount(recordCount);
        page.setData(recordModelList);
        return page;
    }

    @Override
    public Page queryManagerRechargeRecord(Map<String, Object> para, Page page) {
        int offset=(page.getCurrentPage()-1)*page.getPageSize();
        para.put("offset",offset);
        para.put("pageSize",page.getPageSize());
        List<CoinRecordModel> recordModelList=playerDao.queryManagerRechargeRecord(para);
        int recordCount=playerDao.countManagerRechargeRecord(para);
        page.setTotalCount(recordCount);
        page.setData(recordModelList);
        return page;
    }

    @Override
    public Integer countPlayerByCondition( Map<String, Object> para ) {


        return playerDao.countPlayerByCondition(para);
    }

}
